﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using ApplicationGenerator.Versions.BusinessLogic;
using ApplicationGenerator.Versions.Forms;
using System.Collections.Generic;

namespace WorkflowLibrary
{
    /// <summary>
    /// Aktywność do wyświetlania list
    /// </summary>
    [Designer(typeof(ShowListActivityDesigner))]
    public partial class ShowListActivity : SequenceActivity, IActivityName//, ICustomTypeDescriptor, 
    {
        #region Events and event handlers

        public delegate void ShowListEventHandler(object sender,
            ShowListEventArgs e);
        private event ShowListEventHandler listShownEvent;
        public event ShowListEventHandler ListShown
        {
            add
            {
                listShownEvent += value;
            }
            remove
            {
                listShownEvent -= value;
            }
        }

        #endregion

        #region Construction

        public ShowListActivity()
        {
            InitializeComponent();
            ActivityName = "Wyswietl liste";
        }

        #endregion

        #region Dependency properties

        /// <summary>
        /// contains informations about form to be shown
        /// </summary>
        public static DependencyProperty ListVersionInfoProperty =
                    DependencyProperty.Register("ListVersionInfo",
                    typeof(VersionInfo),
                    typeof(ShowListActivity));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("Informacje o wersji")]
        [CategoryAttribute("ShowList Property")]
        [DisplayName("Docelowa lista")]
        [TypeConverter(typeof(VersionConverter))]
        [Editor(typeof(VersionEditor), typeof(UITypeEditor))]
        [VersionType(VersionElement.List)]
        public VersionInfo ListVerisonInfo
        {
            get
            {
                return ((VersionInfo)(base.GetValue(ShowListActivity.ListVersionInfoProperty)));
            }
            set
            {
                base.SetValue(ShowListActivity.ListVersionInfoProperty, value);
            }
        }

        #endregion

        #region Code methods

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            if (listShownEvent != null)
            {
                listShownEvent(null, new ShowListEventArgs(ListVerisonInfo));
            }
        }

        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["ActivityName"]);
            colDisplayProperties.Add(colAvailableCollection["ListVerisonInfo"]);

            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        #region IActivityName Members
        [Category("Dane podstawowe")]
        [DisplayName("Nazwa czynności")]
        [Description("Nazwa czynności przyjazna dla użytkownika")]
        public string ActivityName
        {
            get;
            set;
        }

        #endregion

    }

    #region Event Args class

    public class ShowListEventArgs
    {

        private VersionInfo listVersionInfo;


        public ShowListEventArgs(VersionInfo _listVersionInfo)
        {
            this.listVersionInfo = _listVersionInfo;
        }

        public VersionInfo ListVersionInfo
        {
            get { return listVersionInfo; }
            set { listVersionInfo = value; }
        }
    }

    #endregion
}
